<template>
	<view class="quick-diag">
		<view class="content">
			<!-- 开头 -->
			<view class="diag_title" v-if="dataobj.id">
				<view class="head u-flex u-flex-between">
					<view>{{dataobj.facestatus==1?'已通过':'未通过'}}实名认证</view>
					<view>执业证书编号：{{dataobj.certificate_code}}</view>
				</view>
				<view class="userinfo u-flex">
					<image class="avatar" :src="$base.cdn(dataobj.img,'/static/lj/common/default-doctor.png')" mode="aspectFill"></image>
					<view>
						<view class="tit u-flex u-flex-items-start">
							<view class="u-flex-fill u-flex u-flex-items-end">
								<view class="name">{{dataobj.name}}</view>
								<view class="zc">{{dataobj.academic_title||'暂无职称'}}</view>
							</view>
						</view>
						<view>
							<uni-rate :size="12" activeColor="#ea9518" allow-half :readonly="true" :value="dataobj.score" />
						</view>
						<view class="con">
							擅长：{{dataobj.be_good_at_synopsis||'暂无'}}
							<text class="btn-text-detail" @click="goNext('/pages/info/docinfo?id=' + id,false)">详情</text>
						</view>
					</view>
				</view>
			</view>


			<!-- 就诊人 -->
			<view class="card">
				<view class="title">
					选择就诊人
				</view>
				<view class="seeadoctor">
					<view class="ComPopup_list">
						<view class="ComPopup_listname" :class="item.id==familyobj.id?'listname_a':'listname_b'"
							v-for="(item,index) in familyList" :key="index" @click="Onchooisfamily(item)">
							{{item.the_patient_info&&item.the_patient_info.namea?item.the_patient_info.namea:'--'}}
						</view>
						<view class="ComPopup_listadd" @click="Onjzrgladd">+</view>
					</view>
				</view>
				
				<view class="title">
					需要解读报告信息
				</view>
				<view class="file-box">
					<view class="report-file u-flex u-flex-y-center" v-if="reportobj.report_url">
						<view class="inner u-flex-fill u-flex u-flex-y-center">
							<image src="/static/lj/common/group_712.png" mode="aspectFill"></image>
							<view>
								<view class="tit">{{reportobj.name}}</view>
								<view class="time">{{reportobj.report_time}}</view>
							</view>
						</view>
						<view class="btn-change" @click="gotoSelectFile">更换</view>
					</view>
					<view class="btn-addfile" v-else @click="gotoSelectFile">
						<image src="/static/lj/common/group_763.png" mode="aspectFill"></image>
						<view>选择报告</view>
					</view>
				</view>

				<view class="title">付款信息</view>
				<view class="payInfo">
					<view class="item u-flex u-flex-between">
						<view>报告解读费用</view>
						<view>￥{{consultation_report_money}}</view>
					</view>
					<view class="item coupon u-flex u-flex-between">
						<view>优惠券</view>
						<view>
							<view class="u-flex" v-if="couponobj.id">
								<view class="u-flex" style="margin-right: 10rpx;" @click="gotoSelectCoupon">
									<view>{{couponobj.title}}</view>
									<view class="price">-￥{{couponobj.value}}</view>
								</view>
								<u-icon name="close" @click="unCoupon"></u-icon>
							</view>
							<view class="u-flex" v-else @click="gotoSelectCoupon">
								<view>{{((consultation_report_money||0)<=0||couponnum<=0)?'暂无可用':'选择'}}</view>
								<u-icon name="arrow-right"></u-icon>
							</view>
						</view>
					</view>
					<view class="hjprice u-flex u-flex-end">
						<view>需付款</view>
						<view class="price">￥{{totalPrice}}</view>
					</view>
				</view>

				<view class="xdtip">
					温馨提示：如果您下单成功，医生超过24小时，未接受报告解读，将自动退款至您的账户
				</view>
			</view>
		</view>

		<view class="footer-fixed u-flex u-flex-between">
			<view class="hjprice u-flex">
				<view>合计</view>
				<view class="price">￥{{totalPrice}}</view>
			</view>
			<view class="btn-fillet" @click="Onadd">立即支付</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				familyList: [], //就诊人列表
				familyobj: {}, //默认就诊人
				dataobj: {}, //医生详情
				reportobj: {}, //选中报告
				couponobj: {}, //选中优惠券
				couponnum: 0, //可用优惠券数量

				consultation_report_money: 0,
				totalPrice: 0,
			}
		},
		onLoad: function(options) {
			this.$store.commit('selectReportobj', {})
			if(options.id){
				this.id = options.id

				uni.setNavigationBarTitle({
					title: "报告解读"
				})

				//医生详情
				this.Ondoctordetails()
			}else{
				this.$Http({
					url: 'consultationConsult',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						this.consultation_report_money = res.data.is_sign==1?0:res.data.quick_consultation_report_money
						this.computedTotalPrice()
					}
				})
			}
		},
		onUnload(){
			this.$store.commit('selectReportobj', {})
			this.$store.commit('selectCouponobj', {})
		},
		onShow(){
			//获取就诊人
			this.Onlistmemberfamily()
			
			this.reportobj = this.$store.state.reportobj
			this.couponobj = this.$store.state.couponobj

			this.computedTotalPrice()
		},
		methods: {
			computedTotalPrice(){
				// this.consultation_report_money = this.doctorobj.is_sign==1?0:(this.doctorobj.consultation_report_money || 0)
				if(this.couponobj.least && this.couponobj.least>this.consultation_report_money){
					this.$store.commit('selectCouponobj', {})
					this.couponobj = {}
				}
				
				this.$Http({
					url: 'getCouponSelectCount',
					data: {
						type: 1,
						price: this.consultation_report_money,
					},
					load: false,
				}).then(res=>{
					if(res.code == 1) this.couponnum = res.data;
				});

				var totalPrice = parseFloat(this.consultation_report_money || 0);
				if(this.couponobj.id) totalPrice -= parseFloat(this.couponobj.value || 0);
				if(totalPrice<0) totalPrice = 0;

				this.totalPrice = totalPrice.toFixed(2)
			},
			
			// 取消优惠券
			unCoupon(){
				this.$store.commit('selectCouponobj', {})
				this.couponobj = this.$store.state.couponobj
				this.computedTotalPrice()
			},
			
			//获取就诊人
			Onlistmemberfamily() {
				this.$Http({
					url: 'listThePatient',
					data: {}
				}).then(res => {
					if (!res.data || res.data.length == 0) {} else {
						this.familyList = res.data
						
						var default_familyobj = res.data[0] ? res.data[0] : {};
						
						res.data.forEach(item => {
							item.the_patient_info.namea = item.the_patient_info.name.substring(item
								.the_patient_info.name.length - 2)
							if (item.isdefault == 1) {
								default_familyobj = item
							}
						})
						
						if(!this.familyobj || !this.familyobj.id) this.familyobj = default_familyobj
					}
				})
			},
			//选择就诊人
			Onchooisfamily(e) {
				console.log('Onchooisfamily',e)
				if(e.id==this.familyobj.id) return
				this.familyobj = e
				this.$store.commit('selectReportobj', {})
				this.reportobj = this.$store.state.reportobj
				this.$forceUpdate()
			},
			//添加就诊人
			Onjzrgladd() {
				// this.$store.commit('Onjzkurl', '/pagesL/quick/diagReport?id=' + this.id + '&type=report')
				uni.navigateTo({
					url: '/pagesA/my/jzrgladd',
				})
			},

			//详情
			Ondoctordetails() {
				this.$Http({
					url: 'doctorDetails',
					data: {
						doctor_id: this.id,
					}
				}).then(res => {
					this.dataobj = res.data
					if(this.dataobj.be_good_at_synopsis.length>30){
						this.dataobj.be_good_at_synopsis = this.dataobj.be_good_at_synopsis.substr(0, 30)+'......'
					}
					this.consultation_report_money = this.dataobj.consultation_report_money
					this.computedTotalPrice()
				})
			},
			//提交
			Onadd() {
				if (this.familyobj.id == undefined) {
					uni.showToast({
						title: '请选择就诊人',
						icon: 'none',
						duration: 2000
					})
					return
				}
				
				if (!this.reportobj || !this.reportobj.report_url || this.reportobj.report_url == undefined || this.reportobj.report_url == '') {
					uni.showToast({
						title: '请选择报告',
						icon: 'none',
						duration: 2000
					})
					return
				}

				let data = {
					doctor_id: this.id,
					report_info: JSON.stringify(this.reportobj||{}),
					coupon_id: this.couponobj?this.couponobj.id:0,
					the_patient_id: this.familyobj.the_patient_info?this.familyobj.the_patient_info.id:0,
				}

				this.$Http({
					url: 'createConsultationReportOrder',
					data: data,
					show: true,
					type: 'post'
				}).then(res => {
					if (res.code == 1) {
						if(res.data.pay_code){
							uni.redirectTo({
								url: '/pagesL/my/reportpage?type=1',
							})
						}else{
							// uni.navigateTo({
							// 	url: '/pagesA/ask/payment?id=' + res.data.order_code + '&type=report',
							// })
							// this.paywait(res.data.order_code)
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.pay_data.timeStamp,
								nonceStr: res.data.pay_data.nonceStr,
								package: res.data.pay_data.pacKage,
								signType: res.data.pay_data.signType,
								paySign: res.data.pay_data.paySign,
								success: function(res1) {
									uni.redirectTo({
										url: '/pagesL/my/reportpage?type=1',
									})
								},
								fail: function(err) {
									uni.redirectTo({
										url: '/pagesL/my/reportpage?type=2',
									})
								}
							});
						}
					}
				})

			},
			paywait(order_code) {
				let that = this
				this.$Http({
					url: 'payConsultationReportOrder',
					type:'POST',
					data: {
						order_code: order_code
					},
				}).then(res => {
					if (res.code = 1) {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.pacKage,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(res1) {
								uni.redirectTo({
									url: '/pagesL/my/reportpage?type=1',
								})
							},
							fail: function(err) {
								uni.redirectTo({
									url: '/pagesL/my/reportpage?type=2',
								})
							}
						});
					}
				})
			},

			gotoSelectFile(){
				if (this.familyobj.id == undefined) {
					uni.showToast({
						title: '请选择就诊人',
						icon: 'none',
						duration: 2000
					})
					return
				}
				
				uni.navigateTo({
					url: '/pagesL/my/reportSelect2?id_card='+this.familyobj.the_patient_info.id_card+'&name='+this.familyobj.the_patient_info.name
				})
			},
			gotoSelectCoupon(){
				var price = parseFloat(this.consultation_report_money || 0);
				if(price<=0) return;

				uni.navigateTo({
					url: '/pagesL/my/couponSelect?type=1&price='+price
				})
			},
			// 路由跳转: tag默认false,tag=true跳到tabBar页面
			goNext(url, tag) {
				if (tag) {
					uni.switchTab({
						url: url
					});
				} else {
					uni.navigateTo({
						url: url
					});
				}
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesL/style.less';
	page{background-color: #F6F6F6;}

	.quick-diag .content{
		padding-bottom: 120rpx;
	}
	.quick-diag .content .card{
		padding-bottom: 40rpx;
	}


</style>
